package com.pay.plat.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pay.constant.Constant;
import com.pay.entity.Merchant;
import com.pay.entity.MerchantAccountDetail;
import com.pay.entity.ZfbMember;
import com.pay.enums.MerchantAccountTypeEnums;
import com.pay.plat.annotation.AuthResource;
import com.pay.plat.mapping.MappingUtils;
import com.pay.service.IMerchantAccountDetailService;
import com.pay.service.IMerchantService;
import com.pay.service.IZfbMemberService;
import com.pay.utils.BasePage;
import com.pay.utils.FunctionUtils;
import com.pay.utils.ResponseData;

/**
 * 商户的控制器
 * @author Administrator
 *
 */
@Controller
public class MerchantController {

	@Autowired
	private IMerchantService merchantService ;
	
	@Autowired
	private IMerchantAccountDetailService merchantAccountDetailService ;
	
	@Autowired
	private IZfbMemberService zfbMemberService ;
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showMerchantList)
	public ModelAndView showMerchantList(Merchant merchant,BasePage<Merchant> page,Integer loginId) {
		merchantService.getList(merchant,page) ;
		ModelAndView m = new ModelAndView(MappingUtils.showMerchantList);
		m.addObject("loginId", loginId) ;
		m.addObject("list", page) ;
		m.addObject("merchant", merchant) ;
		return m ;
	}
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showMerchantAdd)
	public ModelAndView showMerchantAdd(Integer loginId) {
		List<ZfbMember> memberList = zfbMemberService.list(new QueryWrapper<ZfbMember>().eq("model", Constant.robto_to_private));
		ModelAndView m = new ModelAndView(MappingUtils.showMerchantAdd);
		m.addObject("loginId", loginId) ;
		m.addObject("memberList", memberList) ;
		return m; 
	}
	
	@AuthResource(url=MappingUtils.showMerchantAdd)
	@ResponseBody
	@RequestMapping(value=MappingUtils.doMerchantAdd,method= {RequestMethod.POST})
	public ResponseData doMerchantAdd(Merchant merchant) {
		return merchantService.doMerchantAdd(merchant) ;
	}
	
	
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showMerchantEdit)
	public ModelAndView showMerchantEdit(Integer loginId,Integer id) {
		Merchant merchant = merchantService.getById(id);
		List<ZfbMember> memberList = zfbMemberService.list(new QueryWrapper<ZfbMember>().eq("model", Constant.robto_to_private));
		ModelAndView m = new ModelAndView(MappingUtils.showMerchantEdit);
		m.addObject("loginId", loginId) ;
		m.addObject("merchant", merchant) ;
		m.addObject("memberList", memberList) ;
		return m ; 
	}
	
	@AuthResource(url=MappingUtils.showMerchantEdit)
	@ResponseBody
	@RequestMapping(value=MappingUtils.doMerchantSwitch,method= {RequestMethod.POST})
	public ResponseData doMerchantSwitch(Integer id) {
		return merchantService.doMerchantSwitch(id) ;
	}
	
	@AuthResource(url=MappingUtils.showMerchantEdit)
	@ResponseBody
	@RequestMapping(value=MappingUtils.doMerchantEdit,method= {RequestMethod.POST})
	public ResponseData doMerchantEdit(Merchant merchant) {
		return merchantService.doMerchantEdit(merchant) ;
	}
	
	
	@AuthResource(url=MappingUtils.doMerchantDelete)
	@ResponseBody
	@RequestMapping(value=MappingUtils.doMerchantDelete,method= {RequestMethod.POST})
	public ResponseData doMerchantDelete(String ids) {
		return merchantService.removeByIds(FunctionUtils.getList(ids.split(","))) ? ResponseData.ok() : ResponseData.baseError("删除失败") ;
	}
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showMerchantAccountDetail)
	public ModelAndView showMerchantAccountDetail(MerchantAccountDetail merchantAccountDetail
			,BasePage<MerchantAccountDetail> basePage,Integer loginId) {
		merchantAccountDetailService.getList(merchantAccountDetail,basePage) ;
		List<Merchant> merchantList = merchantService.list(null);
		
		ModelAndView m = new ModelAndView(MappingUtils.showMerchantAccountDetail);
		m.addObject("loginId", loginId) ;
		m.addObject("list", basePage) ;
		m.addObject("merchantAccountDetail", merchantAccountDetail) ;
		m.addObject("merchantList", merchantList) ;
		return m ; 
	}
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showMerchantAccountPreper)
	public ModelAndView showMerchantAccountPreper(MerchantAccountDetail merchantAccountDetail,Integer loginId) {
		ModelAndView m = new ModelAndView(MappingUtils.showMerchantAccountPreper);
		m.addObject("merchantAccountDetail", merchantAccountDetail) ;
		m.addObject("typeList", MerchantAccountTypeEnums.queryList()) ;
		m.addObject("loginId", loginId) ;
		return m ; 
	}
	
	@ResponseBody
	@AuthResource(url=MappingUtils.showMerchantAccountPreper)
	@RequestMapping(value=MappingUtils.doMerchantAccountPreper,method= {RequestMethod.POST})
	public ResponseData doMerchantAccountPreper(MerchantAccountDetail merchantAccountDetail) {
		return merchantAccountDetailService.doMerchantAccountPreper(merchantAccountDetail) ;
	}
}
